<?php 
if(!isset($_POST['sid']) && !isset($_GET['sid']))
{

	require './hack.php';
}
else
{
	if(!isset($_GET['sid']))
	{
		session_id($_POST['sid']);
	}
	else
	{
		session_id($_GET['sid']);
	}
	$_SESSION['realpage'] = true;
}

// includes

require_once 'class.GeneralServices.php';
session_start();
$_SESSION['realpage'] = true;

// start of page
	$method  =  $_GET['method'];
	
	switch ($method)
	{
		case 'handleavailable':
			$resp=GeneralServices::getService()->isHandleAvailable($_GET['handle']);
			if($resp)
			{
				echo 'true';
			}
			else{
				echo 'false';
			}
			break;
		case 'UpdateUserDetails':
			$resp=GeneralServices::getService()->updateUserInfo($_GET['fname'], $_GET['lname'], $_GET['email'], $_GET['bday'], $_GET['about'], $_SESSION['userObj']->uid);
			if($resp)
			{
				echo 'true';			
			}
			else 
			{
				echo 'false';
			}
			break;	
		case 'setGid':
			$_SESSION['gid'] = $_GET['gid'];
		break;
		case 'setFid':
			$_SESSION['fid'] = $_GET['fid'];
			$_SESSION['gid'] = $_GET['fid'];
		break;
		case 'groupId':
			echo $_SESSION['gid'];
		break;
		case 'goToUserPage':
			$_SESSION['puid'] = $_GET['uid'];
		break;
		case 'PUID':
			echo $_SESSION['gid'];
		break;
		case 'searchGroup':
			$groups =(GeneralServices::getService()->searchGroup($_SESSION['userObj']->uid,$_GET['creteria'],$_GET['limit']));
			if(count($groups) == 0)
			{
				echo '';
			}
			else 
			{
				$rez = '<ol>';
				foreach ($groups as $gr)
				{
					$rez .= "<li class=\"GroupListItem ui-corner-all\" onclick=\"setGroupId($gr->gid);\" style=\"cursor: pointer; font-size: .8em;\">$gr->name</li><br>";
				}			
				$rez .= '</ol>';
				echo $rez;
			}
		break;
		case 'searchFriendDivs':
			$friends =(GeneralServices::getService()->searchFriend($_SESSION['userObj']->uid,$_GET['creteria'],$_GET['limit']));
			if(count($friends) == 0)
			{
				echo '';
			}
			else
			{
				$rez = '';
				foreach ($friends as $fr)
				{
					$rez .= "<div class=\" \" onclick=\"setMsgTarget(".$fr->friendsgroup.",'".$fr->name."');\">$fr->name</div><br>";
				}
				
				echo $rez;
			}
		break;
		case 'createGroup':
			$groupCreated = GeneralServices::getService()->createGroup($_GET['name'], $_GET['desc'], $_GET['visibility'], $_SESSION['userObj']->uid);
			if(is_numeric($groupCreated))
			{
				echo $groupCreated;
			}
			else
			{
				echo 'false';
			}
		break;
		case 'getUsersGroups':
			$usersGroups = GeneralServices::getService()->getUserGroups($_SESSION['userObj']->uid, $_GET['limit'], $_GET['downPour']);
			if(count($usersGroups) == 0 || $usersGroups == false)
			{
				echo '';
			}
			else
			{
				$rez = '<br>';
				foreach ($usersGroups as $ugr)
				{
					$rez .= "<span class=\"GroupListItem ui-corner-all\" onclick=\"setGroupId($ugr->gid);\" style=\"cursor: pointer;\">$ugr->name, $ugr->desc</span><br><br>";
				}
				$rez .= "<input hidden=\"true\" id=\"lastGroupShown\" value=\"".$usersGroups[count($usersGroups)-1]->gid."\"/>";
				echo $rez;
			}
		break;
		case 'getMoreGroups':
			$usersGroups = GeneralServices::getService()->getUserGroups($_SESSION['userObj']->uid, $_GET['limit'], $_GET['downPour']);
			if(count($usersGroups) == 0)
			{
				echo '';
			}
			else
			{
				$rez = '';
				foreach ($usersGroups as $ugr)
				{
					$rez .= "<span class=\"GroupListItem ui-corner-all\" onclick=\"setGroupId($ugr->gid);\" style=\"cursor: pointer;\">$ugr->name, $ugr->desc</span><br><br>";
				}
				$rez .= "<input hidden=\"true\" id=\"lastGroupShown\" value=\"".$usersGroups[count($usersGroups)-1]->gid."\"/>";
				echo $rez;
			}
			break;
		case 'getMorePosts':
			$posts = GeneralServices::getService()->getGroupPosts($_GET['gid'], $_GET['limit'], $_GET['downpour'], $_SESSION['userObj']->uid);
			if(count($posts) == 0)
			{
				echo '';
			}
			else
			{
				$rez = '';
				foreach ($posts as $post)
				{
					$_SESSION['post'] = $post;
					$_SESSION['relation'] = $_GET['relation'];
					ob_start();
					include('./Post.php');
					$rez .= ob_get_clean();										
					unset($_SESSION['post']);
					unset($_SESSION['relation']);
				}
				$rez .= "<input hidden=\"true\" id=\"lastPostShown\" value=\"".$posts[count($posts)-1]->pid."\"/>";
				echo $rez;
			}			
			break;
		
		case 'getMoreMessages':
			$msgs = GeneralServices::getService()->getUserMessages($_SESSION['userObj']->uid,  $_GET['limit'], $_GET['downpour']);
			echo json_encode($msgs); //getUserMessages($uid,$limit,$downpour)
			break;
		
		case 'getThread':
			$msgs = GeneralServices::getService()->getThread($_SESSION['userObj']->uid,$_GET['friend'], $_GET['limit'], $_GET['downpour']);
			echo json_encode($msgs); //getThread($uid,$friend,$limit,$downpour)
			break;
		
		case 'Post':
			$posts = GeneralServices::getService()->publishPost($_SESSION['userObj']->uid, $_GET['gid'], $_GET['content'], $_GET['topic']);
			if($posts)
			{
				echo 'true';
			}
			else
			{
				echo 'false';
			}
			break;
		case 'getTKBKs':
			$TKBKs = GeneralServices::getService()->getPostTalkbacks($_GET['pid'], $_GET['limit'], $_GET['downpour'], $_SESSION['userObj']->uid);
			if(count($TKBKs) == 0)
			{
				echo '';
			}
			else
			{				
				$rez = '';
				$maxID = 0;
				foreach ($TKBKs as $tbk)
				{
					if($tbk->tbid > $maxID)
					{
						$maxID = $tbk->tbid;
					}
					$rez .= "<span id=\"P$tbk->tbid\" class=\"GroupListItem GroupListItemWhole ui-corner-all\"><div><span class=\"talkbackName\">$tbk->user_fname  $tbk->user_lname:&nbsp;</span>$tbk->Content<br/><span class=\"talkbackDate\">$tbk->publishtime</span></div></span><br><br>";
				}			
				$rez .= "<input hidden=\"true\" id=\"".$_GET['pid']."lastTKBKShown\" value=\"".$maxID."\"/>";
				echo $rez;
			}
		break;
		case 'PostTKBK':
			$TKBK = GeneralServices::getService()->publishTalkback($_SESSION['userObj']->uid, $_GET['pid'], $_GET['content']);
			if($TKBK)
			{
				echo 'true';
			}
			else
			{
				echo 'false';
			}
		break;
		case 'hidePost':
			$hide = GeneralServices::getService()->hidePost($_SESSION['userObj']->uid, $_GET['pid']);
			if($hide)
			{
				echo 'true';
			}
			else
			{
				echo 'false';
			}
		break;
		case 'showPost':
			$show = GeneralServices::getService()->showPost($_SESSION['userObj']->uid, $_GET['pid']);
			if($show)
			{
				echo 'true';
			}
			else
			{
				echo 'false';
			}
		break;
		case 'sendMsg':					
			$show = GeneralServices::getService()->sendMessage($_SESSION['userObj']->uid, $_GET['friendid'],$_GET['content']);
			if($show)
			{
				echo 'true';
			}
			else
			{
				echo 'false';
			}
		break;
		case 'loadGroupMembers':
			$groupMembers = GeneralServices::getService()->getGroupUsers($_GET['gid']);
			if(count($groupMembers) == 0 || $groupMembers == null)
			{
				echo '';
			}
			else
			{
				$mem = '';
				foreach ($groupMembers as $memb)
				{
					$mem .= "<span class=\"GroupListItem ui-corner-all\" onclick=\"goToUserPage($memb->uid);\" style=\"cursor: pointer;\">$memb->FirstName , $memb->Surname</span><span class=\"GroupListItem ui-corner-all\" style=\"cursor: pointer;\" id=\"".$memb->uid."uninvite\" onclick=\"InviteToGroup($memb->uid, 'false')\">UnInvite</span><br><br>"; 
				}
				echo $mem;
			}			
		break;
		case 'loadGroupWBMembers':
			$groupMembers = GeneralServices::getService()->getGroupWBUsers($_GET['gid']);
			if(count($groupMembers) == 0 || $groupMembers == null)
			{
				echo '';
			}
			else
			{
				$mem = '';
				foreach ($groupMembers as $memb)
				{
					$mem .= "<span class=\"GroupListItem ui-corner-all\">$memb->FirstName , $memb->Surname</span><span class=\"GroupListItem ui-corner-all\" id=\"".$memb->uid."invite\" onclick=\"InviteToGroup($memb->uid, 'true')\" style=\"cursor: pointer; width: 40px;\">Invite</span><span class=\"GroupListItem ui-corner-all\" style=\"visibility: visible; display: none; cursor: pointer;\" id=\"".$memb->uid."uninvite\" onclick=\"InviteToGroup($memb->uid, 'false')\">UnInvite</span>"; 
					//"<span class=\"GroupListItem ui-corner-all\" onclick=\"goToUserPage($memb->uid);\" style=\"cursor: pointer;\">$memb->FirstName , $memb->Surname</span><br><br>";
				}
				echo $mem;
			}
		break;
		case 'searchFriend':
			$friends =(GeneralServices::getService()->searchFriend($_SESSION['userObj']->uid,$_GET['creteria'],$_GET['limit']));
			if(count($friends) == 0)
			{
				echo '';
			}
			else
			{
				$rez = '<ol>';
				foreach ($friends as $fr)
				{
					if(isset($_GET['invite']) && $_GET['invite'] == 'true')
					{
						$rez .= "<li class=\"GroupListItem ui-corner-all\">$fr->name <span id=\"".$fr->friendsgroup."invite\" onclick=\"InviteToGroup($fr->friendsgroup, 'true')\" style=\"cursor: pointer;\">Invite</span><span style=\"visibility: visible; display: none; cursor: pointer;\" id=\"".$fr->friendsgroup."uninvite\" onclick=\"InviteToGroup($fr->friendsgroup, 'false')\">UnInvite</span>";
					}
					else
					{
						$rez .= "<li class=\"GroupListItem ui-corner-all\" onclick=\"setFriendId($fr->gid);\" style=\"cursor: pointer;\">$fr->name</li><br>";
					}
				}
				$rez .= '</ol>';
				echo $rez;
			}
		break;
		case 'inviteToGroup':
			if(!isset($_GET['gid']))
			{
				$wall = GeneralServices::getService()->getUserswall($_SESSION['userObj']->uid, $_SESSION['userObj']->uid);
				$_GET['gid'] = $wall->gid;
			}
			if($_GET['val'] == 'true')
			{
				$show = GeneralServices::getService()->inviteUserToGroup($_SESSION['userObj']->uid,  $_GET['gid'], $_GET['uid']);
			}
			else
			{
				$show = GeneralServices::getService()->banFromGroup($_SESSION['userObj']->uid, $_GET['gid'], $_GET['uid']);
			}
			if($show)
			{
				echo 'true';
			}
			else
			{
				echo 'false';
			}	
		break;
		case 'FriendUnFriend':
			if($_GET['val'] == 'true')
			{
				$inv = GeneralServices::getService()->friend($_SESSION['userObj']->uid, $_GET['uid']);
			}
			else
			{
				$inv = GeneralServices::getService()->unfriend($_SESSION['userObj']->uid, $_GET['uid']);
			}
			if($inv)
			{
				echo 'true';
			}
			else
			{
				echo 'false';
			}
		break;
		case 'GoToWall':
			$wall = GeneralServices::getService()->getUserswall($_SESSION['userObj']->uid, $_GET['uid']);
			if(is_object($wall))
			{
				echo $wall->gid;
			}
			else
			{
				echo 'false';
			}
		break;
		case 'LoadUserFriends':
			$friends =(GeneralServices::getService()->getUserRealFriends($_SESSION['userObj']->uid, PHP_INT_MAX, 0));
			if(count($friends) == 0)
			{
				echo '';
			}
			else
			{
				$rez = '<ul>';
				foreach ($friends as $fr)
				{
					if(isset($_GET['invite']) && $_GET['invite'] == 'true')
					{
						$rez .= "<li class=\"GroupListItem ui-corner-all\">$fr->name <span id=\"".$fr->friendsgroup."invite\" onclick=\"InviteToGroup($fr->friendsgroup, 'true')\" style=\"cursor: pointer;\">Invite</span><span style=\"visibility: visible; display: none; cursor: pointer;\" id=\"".$fr->friendsgroup."uninvite\" onclick=\"InviteToGroup($fr->friendsgroup, 'false')\">UnInvite</span>";
					}
					else
					{
						$rez .= "<li class=\"GroupListItem ui-corner-all\" style=\"width: 25%;\" onclick=\"setFriendId($fr->gid);\" style=\"cursor: pointer;\">$fr->name</li><div style=\"margin: 10px;\"><span id=\"".$fr->friendsgroup."invite\" class=\"GroupListItem ui-corner-all\" onclick=\"FriendUnFriend($fr->friendsgroup, 'true')\" style=\"visibility: visible; display: none; cursor: pointer;\">Approve</span><span style=\"cursor: pointer;\" class=\"GroupListItem ui-corner-all\" id=\"".$fr->friendsgroup."uninvite\" onclick=\"FriendUnFriend($fr->friendsgroup, 'false')\">UnFriend</span></div><br>";
					}
				}
				$rez .= '</ul>';
				echo $rez;
			}
		break;
		case 'LoadUserWBFriends':
			$friends =(GeneralServices::getService()->getUserWannabeFriends($_SESSION['userObj']->uid, PHP_INT_MAX, 0));
			if(count($friends) == 0)
			{
				echo '';
			}
			else
			{
				$rez = '<ul>';
				foreach ($friends as $fr)
				{
					if(isset($_GET['invite']) && $_GET['invite'] == 'true')
					{
						$rez .= "<li class=\"GroupListItem ui-corner-all\">$fr->name <span id=\"".$fr->friendsgroup."invite\" onclick=\"FriendUnFriend($fr->friendsgroup, 'true')\" style=\"cursor: pointer;\">Invite</span><span style=\"visibility: visible; display: none; cursor: pointer;\" id=\"".$fr->friendsgroup."uninvite\" onclick=\"FriendUnFriend($fr->friendsgroup, 'false')\">UnInvite</span>";
					}
					else
					{
						$rez .= "<li class=\"GroupListItem ui-corner-all\" style=\"width: 25%;\">$fr->name</li><div style=\"margin: 10px;\"><span id=\"".$fr->friendsgroup."invite\" onclick=\"FriendUnFriend($fr->friendsgroup, 'true')\" class=\"GroupListItem ui-corner-all\" style=\"cursor: pointer;\">Approve</span><span style=\"visibility: visible; display: none; cursor: pointer;\" class=\"GroupListItem ui-corner-all\" id=\"".$fr->friendsgroup."uninvite\" onclick=\"FriendUnFriend($fr->friendsgroup, 'false')\">Ban</span></div>";
					}
				}
				$rez .= '</ul>';
				echo $rez;
			}
		break;
		case 'LoadUserBannedFriends':
			$friends =(GeneralServices::getService()->getUserBannedFriends($_SESSION['userObj']->uid, PHP_INT_MAX, 0));
			if(count($friends) == 0)
			{
				echo '';
			}
			else
			{
				$rez = '<ul>';
				foreach ($friends as $fr)
				{
					if($_GET['invite'] == 'true')
					{
						$rez .= "<li class=\"GroupListItem ui-corner-all\">$fr->name <span id=\"".$fr->friendsgroup."invite\" onclick=\"InviteToGroup($fr->friendsgroup, 'true')\" style=\"cursor: pointer;\">Invite</span><span style=\"visibility: visible; display: none; cursor: pointer;\" id=\"".$fr->friendsgroup."uninvite\" onclick=\"InviteToGroup($fr->friendsgroup, 'false')\">UnInvite</span>";
					}
					else
					{
						$rez .= "<li class=\"GroupListItem ui-corner-all\" style=\"width: 25%;\">$fr->name</li><div style=\"margin: 10px;\"><span id=\"".$fr->friendsgroup."invite\" class=\"GroupListItem ui-corner-all\" onclick=\"InviteToGroup($fr->friendsgroup, 'true')\" style=\"cursor: pointer;\">Approve</span><span style=\"visibility: visible; display: none; cursor: pointer;\" class=\"GroupListItem ui-corner-all\" id=\"".$fr->friendsgroup."uninvite\" onclick=\"InviteToGroup($fr->friendsgroup, 'false')\">Ban</span></div>";
					}
				}
				$rez .= '</ul>';
				echo $rez;
			}
		break;
		case 'applyToGroup':
			$response =(GeneralServices::getService()->applyForGroup($_SESSION['userObj']->uid, $_GET['gid']));
			if($response)
			{
				echo 'true';
			}
			else
			{
				echo 'false';
			}
		break;
		case 'fillDB':
			if(isset($_SESSION['admin']) &&  $_SESSION['admin']== true){
				require "./FillerSciprt.php";
				echo "true";
			}
			else {
				echo "false";
			}
			break;
		default:
			require './hack.php';
			break;
	}



// end of page logic



$_SESSION['realpage'] = false;
?>